#include <iostream>

#include "date.h"


namespace Date_module{
    Date::Date(int year, int month, int day)
    : _year(year)
    , _month(month)
    , _day(day){
    }

    Date::Date(const Date& date)
    :_year(date._year)
    ,_month(date._month)
    ,_day(date._day){
    }


    // Compare set
    
    bool Date::operator>(const Date& d) const
    {
        if(this->_year < d._year){
            return false;
        }
        else if(this->_year == d._year){
            if(this->_month < d._month){
                return false;
            }
            else if(this->_month == d._month){
                return this->_day > d._day;
            }
        }
        return true;
    }
    bool Date::operator==(const Date& d) const
    {
        return this->_year == d._year 
        && this->_month == d._month
        && this->_day == d._day;
    }
    bool Date::operator<(const Date& d) const
    {
        return !(*this >= d);
    }
    bool Date::operator>=(const Date& d) const
    {
        return (*this > d) || (*this == d);
    }
    bool Date::operator<=(const Date& d) const
    {
        return (*this < d) || (*this == d);
    }
    bool Date::operator!=(const Date& d) const
    {
        return !(*this == d);
    }

    // Calculator: reuse 「+=」 rather than 「+」
    
    Date& Date::operator+=(int day_counting){
        if(day_counting < 0) return *this;
        day_counting += _day;
        int month_day = month2day(_year, _month);
        while(day_counting > month_day){
            day_counting -= month_day;
            ++_month;
            if(_month == 13) {
                ++_year;
                _month = 1;
            }
            month_day = month2day(_year, _month);
        }
        _day = day_counting; // 记得更新 _day
        return *this;
    }
    Date Date::operator+(int day_counting) const{
        Date tmp = *this;
        tmp += day_counting;
        return tmp;
    }

    Date Date::operator++(int){
        Date tmp = *this;
        *this += 1;
        return tmp;
    }
    Date& Date::operator++(){
        *this += 1;
        return *this;
    }

    Date& Date::operator--(){
        *this -= 1;
        return *this;
    }
    Date Date::operator--(int){
        Date tmp = *this;
        --*this;
        return tmp;
    }
    Date Date::operator-(int day_counting) const{
        Date tmp = *this;
        tmp -= day_counting;
        return tmp;
    }
    Date& Date::operator-=(int day_counting){
        _day -= day_counting;
        while(_day <= 0){
            --_month;
            if(_month == 0){
                _month = 12;
                --_year;    
            }
            _day += month2day(_year, _month);
        }
        return *this;
    }
    int Date::operator-(const Date& date) const{
        int flag = 1;
        if(*this < date){
            flag = -1;
        }
        int ret = 0;
        Date tmp = date;
        while(tmp != *this){
            ++tmp;
            ++ret;
        }
        return ret * flag;
    }

    // std::ostream operator<<(std::ostream out, const Date& date){
    std::ostream& operator<<(std::ostream& out, const Date& date){
        out << date._year << '-' << date._month << '-' << date._day;
        return out;
    }
    std::istream& operator>>(std::istream& in, Date& date){
        std::cout << "请输入日期" << std::endl;
        in >> date._year >> date._month >> date._day;
        return in;
    }
}
